#############################################################################
##
## Copyright (C) 2006-2006 Mindcast Productions. All rights reserved.
##
## This file is part of the Universal SQL Navigator - UniSQLNav.
## Find more information in: http://code.google.com/p/unisqlnav/
## Contact at carlosedp@gmail.com
##
## This file may be used under the terms of the GNU General Public
## License version 2.0 as published by the Free Software Foundation
## and appearing in the file LICENSE.GPL included in the packaging of
## this file.  Please review the following information to ensure GNU
## General Public Licensing requirements will be met:
## http://www.gnu.org/licenses/gpl.txt
##
## This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
## WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
##
## $Rev: $
## $Date: $
##
#############################################################################

from PyQt4 import QtCore, QtGui, Qsci

from utils import QTUtilities
from ui.internalWindow import internalWindowTemplate
from ui.ui_tnswindow import Ui_tnswindow

class tnsWindow(internalWindowTemplate, Ui_tnswindow):
    def __init__(self, parent, TNS):
        internalWindowTemplate.__init__(self, parent)

        self.TNS = TNS
        self.connect(self.loadButton, QtCore.SIGNAL("clicked()"),self.loadTNS)
        self.connect(self.cancelButton, QtCore.SIGNAL("clicked()"),self.closeWindow)
        self.connect(self.tnstable, QtCore.SIGNAL("itemDoubleClicked(QTableWidgetItem*)"),self.loadTNS)
        self.loadTNSTable()
        
    def loadTNSTable(self):
        self.tnstable.setSortingEnabled(False)
        for row in xrange(self.tnstable.rowCount()):
            self.tnstable.removeRow(0)
        self.tnstable.verticalHeader().hide()
        tableColumns = ["TNSName", "Host", "Port", "SID"]
        self.tnstable.setColumnCount(len(tableColumns))
        self.tnstable.setHorizontalHeaderLabels(tableColumns)
        for tns in self.TNS:
            row = self.tnstable.rowCount()
            self.tnstable.insertRow(row)
            fieldmap = {'HOST': 'HOST','PORT': 'PORT','NAME': 'NAME', 'SID': 'NAME'}
            data = {}
            for key in self.TNS[tns]:
                for field in fieldmap:
                    if key.upper() == field.upper():
                        data[fieldmap[field]] = self.TNS[tns][key]
            data['TNS'] = tns
            itemData = [data['TNS'], data['HOST'],data['PORT'],data['NAME']]
            for col, data in enumerate(itemData):
                self.tnstable.setItem(row, col, QtGui.QTableWidgetItem(str(data)))

        self.tnstable.setSortingEnabled(True)
        self.tnstable.sortItems(0)
        self.tnstable.resizeColumnsToContents()
        self.tnstable.resizeRowsToContents()
            

    def loadTNS(self):
        tns = self.tnstable.item(self.tnstable.currentRow(),0).text()
        try:
            from ui.sessionWindow import sessionWindow
            self.parent.openwindowsNames[sessionWindow].database.setText(tns)
        except: pass
        self.close()

    def closeWindow(self):
        self.close()
        
